package servlet;

import java.io.IOException;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.*;
import service.NewsService;



public class NewsController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private String action;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost( request,  response);
	}
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        action = request.getParameter("action");
        switch (action) {
            case "toShowNews":
            	toShowNews(request, response);
                break;
            case "toShowNewsList":
            	toShowNewsList(request, response);
                break;
            default:
                break;
        }
    }

    protected void toShowNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        NewsBean newsBean = new NewsService().getNews(id);
		request.setAttribute("newsBean",newsBean);
		request.getRequestDispatcher("ShowNews.jsp").forward(request, response);
    }

    
    protected void toShowNewsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        List<NewsBean> newsList = new NewsService().query_all_news();
		request.setAttribute("newsList",newsList);
		request.getRequestDispatcher("ShowNewsList.jsp").forward(request, response);
    }
    
   

}
